﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.Administration.Models;
using Fuse8.ContentManagementFramework.BizFacade.Workflow;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.SystemFacade;
using Fuse8.ContentManagementFramework.Web.ApplicationRuntime;
using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.Domain;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public class CmsWorkflowController : CmsAdminController
    {
        private CmsEntityRevisionService<CmsPage> _pageRevisionService;

        protected virtual CmsEntityRevisionService<CmsPage> PageService
        {
            get
            {
                if (_pageRevisionService == null)
                {
                    _pageRevisionService = new CmsPageService();
                }

                return _pageRevisionService;
            }
            set
            {
                _pageRevisionService = value;
            }
        }

        public virtual ActionResult Index()
        {
            var data = new List<CmsDashboard>();

            data.Add(
                new CmsDashboard
                {
                    DashboardName = "Draft items",
                    LoadUrl = "/admin/cmsworkflow/draftworkflowgrid",
                    Width = 640
                });
            data.Add(
                new CmsDashboard
                {
                    DashboardName = "Active items",
                    LoadUrl = "/admin/cmsworkflow/activeworkflowgrid",
                    Width = 940
                });
            data.Add(
                new CmsDashboard
                {
                    DashboardName = "Approved items",
                    LoadUrl = "/admin/cmsworkflow/approvedworkflowgrid",
                    Width = 640
                });

            return View(data);
        }

        public virtual ActionResult DraftWorkflowGrid()
        {
            var data = PageService.FindAllDraftRevisions();

            var result = (data != null) && data.Any() ?
                data.Select(p => new CmsAdminWorkflowDraftModel
                {
                    Id = p.Id,
                    EntityType = p.TypeParameter,
                    Title = p.PageName
                }) :
                new List<CmsAdminWorkflowDraftModel>();

            return PartialView(result);
        }

        public virtual ActionResult ActiveWorkflowGrid()
        {
            var data = PageService.FindAllActiveRevisions();

            var result = (data != null) && data.Any() ?
                data.Select(p => new CmsAdminWorkflowActiveModel
                {
                    Id = p.Id,
                    EntityType = p.TypeParameter,
                    Title = p.PageName,
                    ApprovalDate = p.Workflow.ApproveDateDeadline,
                    Cycle = p.Workflow.Cycle,
                    State = p.Workflow.LatestWorkflowState.WorkflowState,
                    Status = p.Workflow.LatestWorkflowState.WorkflowState.ToString(),
                    UserGroupId = p.Workflow.LatestWorkflowState.UserGroupId,
                    UserId = p.Workflow.LatestWorkflowState.UserId,
                    UserGroup = p.Workflow.LatestWorkflowState.AssignedUserGroup,
                    User = p.Workflow.LatestWorkflowState.AssignedUser
                }) :
                new List<CmsAdminWorkflowActiveModel>();

            return PartialView(result);
        }

        public virtual ActionResult ApprovedWorkflowGrid()
        {
            var data = PageService.FindAllApprovedRevisions();

            var result = (data != null) && data.Any() ?
                data.Select(p => new CmsAdminWorkflowApprovedModel
                {
                    Id = p.Id,
                    EntityType = p.TypeParameter,
                    Title = p.PageName
                }) :
                new List<CmsAdminWorkflowApprovedModel>();

            return PartialView(result);
        }

        [HttpPost]
        public virtual ActionResult StartWorkflow(IEnumerable<CmsAdminWorkflowDraftModel> entities)
        {
            foreach (var draft in entities)
            {
                var service = DependencyServiceLocator.Current.GetInstance<ICmsEntityRevisionService>(draft.EntityType);

                service.StartWorkflow(draft.Id, null, null);
            }

            return Json(0);
        }

        [HttpPost]
        public virtual ActionResult MassDelete(IEnumerable<CmsAdminWorkflowDraftModel> entities)
        {
            foreach (var entity in entities)
            {
                var service = DependencyServiceLocator.Current.GetInstance<ICmsEntityRevisionService>(entity.EntityType);

                service.Remove(entity.Id);
            }

            CmsRoute.ClearByDependency();

            // TODO exception handling
            return Json(0);            
        }

        [HttpPost]
        public virtual ActionResult Withdraw(IEnumerable<CmsAdminWorkflowDraftModel> entities)
        {
            foreach (var entity in entities)
            {
                var service = DependencyServiceLocator.Current.GetInstance<ICmsEntityRevisionService>(entity.EntityType);

                service.Withdraw(entity.Id);
            }

            CmsRoute.ClearByDependency();

            return Json(0);
        }


    }
}
